<?php

namespace App\Services;

use App\Services\WanSdk\WanSdkService;

class SdkService extends ServiceBase
{
    /**
     * 获得签名
     * @param  string $appkey 签名key
     * @param  array  $params 待签名参数
     * @return string
     */
    public function getSign($appkey, $params)
    {
        $arr = [];
        ksort($params);
        foreach ($params as $key => $val) {
            if ($key === 'sign') continue;
            if ($val === '' || $val === 'null' || $val === 'undefined' || is_null($val)) continue;
            $arr[] = "$key=$val";
        }
        $str  = implode("&", $arr) . "$appkey";
        $sign = md5(strtolower($str));

        return $sign;
    }

    public function checkSign($params)
    {
        return true;
        if (!isset($params['sign'])) {
            return false;
        }

        $sign       = $params['sign'];
        $check_sign = $this->createSign($params);
        if (strtolower($sign) !== strtolower($check_sign)) {
            return false;
        }

        return true;
    }

    /**
     * 创建内部前后端校验的sign
     * @param  array $params 待签名参数
     * @return string
     */
    public function createSign(array $params)
    {
        $appkey = config('my.appkey');
        $sign   = $this->getSign($appkey, $params);

        return $sign;
    }

    // 补充设备数据到模型
    public function supplementDevice($model, array $params)
    {
        $model->net_work    = $params['net_work'] ?? '';
        $model->sdk_version = $params['sdk_version'] ?? '';

        $device_os = $params['device_os'] ?? '';
        if (strlen($device_os) > 31) {
            $device_os = substr($device_os, 0, 31);
        }


        $model->device_factory  = $params['device_factory'] ?? '';
        $model->device_os       = $device_os;
        $model->device_system   = $params['device_system'] ?? '';
        $model->device_screen_w = $params['device_screen_w'] ?? 0;
        $model->device_screen_h = $params['device_screen_h'] ?? 0;

        $model->device_num  = $params['device_num'] ?? '';
        $model->idfa        = $params['idfa'] ?? '';
        $model->idfv        = $params['idfv'] ?? '';
        $model->imei        = $params['imei'] ?? '';
        $model->android_id  = $params['android_id'] ?? '';
        $model->google_adid = $params['google_adid'] ?? '';
        $model->oaid        = $params['oaid'] ?? '';

        $model->device_type = $params['device_type'] ?? 0;
        if (empty($model->device_type)) {
            $model->device_type = WanSdkService::getInstance()->getPhoneType();
        }

        return true;
    }

    // 创建CP方校验的登录sign
    public function createCpLoginSign(string $appkey, array $params): string
    {
        return $this->getSign($appkey, $params);
    }

    // 创建CP方校验的充值sign
    public function createCpPaySign(string $appsecret, array $params): string
    {
        return $this->getSign($appsecret, $params);
    }

    // 切支付地址，给与前端通过webview拉起
    public function switchPayUrl($order): string
    {
        $trade_no = $order->trade_no;
        return config('my.switch_pay_url') . "/pay/index/" . $trade_no;
    }
}
